﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Globalization;
using System.Web.UI.HtmlControls;

namespace MpSoft.Basic.Utils
{
    public class SEOHelper
    {
        #region Methods
        /// <summary>
        /// Renders page meta tag
        /// </summary>
        /// <param name="page">Page instance</param>
        /// <param name="name">Meta name</param>
        /// <param name="content">Content</param>
        /// <param name="OverwriteExisting">Overwrite existing content if exists</param>
        public static void RenderMetaTag(Page page, string name, string content, bool OverwriteExisting)
        {
            foreach (Control control in page.Header.Controls)
                if (control is HtmlMeta)
                {
                    HtmlMeta meta = (HtmlMeta)control;
                    if (meta.Name.ToLower().Equals(name.ToLower()) && !string.IsNullOrEmpty(content))
                    {
                        if (OverwriteExisting)
                            meta.Content = content;
                        else
                        {
                            if (String.IsNullOrEmpty(meta.Content))
                                meta.Content = content;
                        }
                    }
                }
        }

        /// <summary>
        /// Renders page title
        /// </summary>
        /// <param name="page">Page instance</param>
        /// <param name="title">Page title</param>
        /// <param name="OverwriteExisting">Overwrite existing content if exists</param>
        public static void RenderTitle(Page page, string title, bool OverwriteExisting)
        {
            if (String.IsNullOrEmpty(title))
                return;

            if (OverwriteExisting)
                page.Title = title;
            else
            {
                if (String.IsNullOrEmpty(page.Title))
                    page.Title = title;
            }
        }

        /// <summary>
        /// Murges SE name
        /// </summary>
        /// <param name="name">Name</param>
        /// <returns>Result</returns>
        public static string MungeSEName(string name)
        {
            if (String.IsNullOrEmpty(name))
                return string.Empty;
            string OKChars = "abcdefghijklmnopqrstuvwxyz1234567890 _-";
            name = name.Trim().ToLowerInvariant();
            StringBuilder tmpS = new StringBuilder();
            foreach (char c in name.ToCharArray())
                if (OKChars.Contains(c.ToString()))
                    tmpS.Append(c);
            string name2 = tmpS.ToString();
            name2 = name2.Replace(" ", "-");
            while (name2.Contains("--"))
                name2 = name2.Replace("--", "-");
            while (name2.Contains("__"))
                name2 = name2.Replace("__", "_");
            return HttpContext.Current.Server.UrlEncode(name2);
        }

        public static string GetCategoryUrl(string CategorySeName)
        {
            return string.Format("/{0}.html", CategorySeName);
        }

        public static string GetDetailUrl(string CategorySeName, string Title, string itemID)
        {
            //return string.Format("{0}/{1}.aspx", CommonHelper.GetStoreHTTPLocation(false), cateCode, title, itemID);
            return string.Format("/{0}/{1}-{2}.html", CategorySeName, Title, itemID);
        }

        public static string UrlSenameTitle(string CategorySeName, string Title)
        {
            return "/" + CategorySeName + "/" + Title + ".html";
        }

        public static string FormatUrl(string CategorySeName, string Title)
        {
            return "/" + CategorySeName + "/" + Title + "-{0}.html";
        }
     
        #endregion
    }
}
